package com.dmap.admin.controller.backgroundv3;

import com.dmap.admin.constants.VersionConstant;
import com.dmap.admin.controller.backgroundv3.param.FirmwareVersionEditParam;
import com.dmap.admin.controller.backgroundv3.param.FirmwareVersionParam;
import com.dmap.admin.controller.backgroundv3.param.FirmwareVersionPublishParam;
import com.dmap.admin.po.FirmwareVersion;
import com.dmap.admin.service.backgroundv3.FirmwareVersionService;
import com.dmap.admin.vo.backgroundv3.FirmwareVersionInfoVo;
import com.dmap.admin.vo.backgroundv3.FirmwareVersionSelectVo;
import com.dmap.base.constants.Constant;
import com.dmap.base.vo.BaseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: zmh
 * @create: 18-8-22
 * @description: 固件版本Controller
 **/
@Api(value = "固件版本", description = "background/固件版本")
@RestController
@RequestMapping(value = "/background/dmap-knowledge-server/v1/firmwareVersion/")
public class FirmwareBackController {
    @Resource
    private FirmwareVersionService firmwareVersionService;

    @ResponseBody
    @ApiOperation(value = "新增固件版本", notes = "新增固件版本")
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public BaseVo insertFirmwareVersion(@RequestBody FirmwareVersionParam param) {
        BaseVo result = new BaseVo();
        if (StringUtils.isBlank(param.getFirmwareNumber()) || StringUtils.isBlank(param.getDescription()) || StringUtils.isBlank(param.getDescriptionEn()) ||
                StringUtils.isBlank(param.getJsonUrl()) || StringUtils.isBlank(param.getMd5()) || StringUtils.isBlank(param.getMinVersion()) ||
                StringUtils.isBlank(param.getMinVersionCode()) || StringUtils.isBlank(param.getNewVersion()) || StringUtils.isBlank(param.getNewVersionCode()) ||
                param.getStatus() == 0 || StringUtils.isBlank(param.getUrl())) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        int success = firmwareVersionService.insertFirmwareVersionVersion(param);
        if (success == 0) {
            result.setCode(Constant.RESPONSE_INSERT_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_INSERT_ERROR_MSG);
        }
        return result;
    }

    @ResponseBody
    @ApiOperation(value = "修改固件版本", notes = "修改固件版本")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public BaseVo updateFirmwareVersion(@RequestBody FirmwareVersionEditParam param) {
        BaseVo result = new BaseVo();
        if (StringUtils.isBlank(param.getFirmwareNumber()) || !StringUtils.isNumeric(param.getFirmwareVersionId()) || StringUtils.isBlank(param.getFirmwareVersionId()) || StringUtils.isBlank(param.getDescription()) || StringUtils.isBlank(param.getDescriptionEn()) ||
                StringUtils.isBlank(param.getJsonUrl()) || StringUtils.isBlank(param.getMd5()) || StringUtils.isBlank(param.getMinVersion()) ||
                StringUtils.isBlank(param.getMinVersionCode()) || StringUtils.isBlank(param.getNewVersion()) || StringUtils.isBlank(param.getNewVersionCode()) ||
                param.getStatus() == 0 || StringUtils.isBlank(param.getUrl())) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        int success = firmwareVersionService.updateFirmwareVersion(param);
        if (success == 0) {
            result.setCode(Constant.RESPONSE_UPDATE_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_UPDATE_ERROR_MSG);
        }
        if (success == 2) {
            result.setCode(VersionConstant.RESPONSE_FIRMWARE_VERSION_EXIST_ERROR_CODE);
            result.setMsg(VersionConstant.RESPONSE_FIRMWARE_VERSION_EXIST_ERROR_MSG);
        }
        return result;
    }

    @ResponseBody
    @ApiOperation(value = "发布固件版本", notes = "发布固件版本")
    @RequestMapping(value = "/publish", method = RequestMethod.POST)
    public BaseVo updateFirmwareVersionPublish(@RequestBody FirmwareVersionPublishParam param) {
        BaseVo result = new BaseVo();
        if (!StringUtils.isNumeric(param.getFirmwareVersionId()) || StringUtils.isBlank(param.getFirmwareVersionId())
                || !StringUtils.isNumeric(param.getPromulgatorId()) || StringUtils.isBlank(param.getPromulgatorId())
        ) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        int success = firmwareVersionService.updateFirmwareVersionPublish(param);
        if (success == 0) {
            result.setCode(Constant.RESPONSE_UPDATE_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_UPDATE_ERROR_MSG);
        }
        if (success == 2) {
            result.setCode(VersionConstant.RESPONSE_FIRMWARE_VERSION_EXIST_ERROR_CODE);
            result.setMsg(VersionConstant.RESPONSE_FIRMWARE_VERSION_EXIST_ERROR_MSG);
        }
        return result;
    }

    @Transactional
    @ResponseBody
    @ApiOperation(value = "删除固件版本", notes = "删除固件版本")
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public BaseVo deleteFirmwareVersion(@RequestParam String firmwareVersionIds) {
        BaseVo result = new BaseVo();
        if (StringUtils.isBlank(firmwareVersionIds)) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        int success = firmwareVersionService.deleteFirmwareVersionList(firmwareVersionIds);
        if (success == 0) {
            result.setCode(Constant.RESPONSE_DELETE_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_DELETE_ERROR_MSG);

        }
        return result;

    }

    @ResponseBody
    @ApiOperation(value = "查询固件版本", notes = "查询固件版本")
    @RequestMapping(value = "/select", method = RequestMethod.GET)
    public BaseVo selectFirmwareVersion(@RequestParam(required = false) String companyNumber,
                                        @RequestParam(required = false) String appNumber,
                                        @RequestParam String lang,
                                        @RequestParam(required = false) String columnId,
                                        @RequestParam(required = false) String key,
                                        @RequestParam(required = false, defaultValue = "0") long order,
                                        @RequestParam int pageIndex,
                                        @RequestParam int pageSize) {
        BaseVo result = new BaseVo();
//        if (lang == null || pageIndex == 0 || pageSize == 0) {
//            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
//            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
//            return result;
//        }
//        if (appNumber != null && StringUtils.isBlank(appNumber)) {
//            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);

//            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
//            return result;
//        }
//        if (companyNumber != null && StringUtils.isBlank(companyNumber)) {
//            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
//            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
//            return result;
//        }
//        if (columnId != null && !StringUtils.isNumeric(columnId)) {
//            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
//            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
//            return result;
//        }
//        if (StringUtils.isBlank(lang)) {
//            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
//            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
//            return result;
//        }
//        ArticleBackSelectAll articleBackSelectAll = new ArticleBackSelectAll();
//        articleBackSelectAll.setArticleList(articleExtensionService.selectArticleList(companyNumber, appNumber, lang, columnId, key, order, pageIndex, pageSize));
//        articleBackSelectAll.setCount(articleExtensionService.countArticleList(companyNumber, appNumber, lang, columnId, key, order));
//        result.setData(articleBackSelectAll);
        List<FirmwareVersion> firmwareVersionList = firmwareVersionService.selectFirmwareVersion();
        List<FirmwareVersionSelectVo> firmwareVersionSelectVoList = new ArrayList<>();
        for (FirmwareVersion firmwareVersion : firmwareVersionList){
            FirmwareVersionSelectVo firmwareVersionSelectVo = new FirmwareVersionSelectVo(firmwareVersion);
            firmwareVersionSelectVoList.add(firmwareVersionSelectVo);
        }
        return result;
    }

    @ResponseBody
    @ApiOperation(value = "查看固件版本信息", notes = "查看固件版本信息")
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public BaseVo getFirmwareVersion(@RequestParam String firmwareVersionId) {
        BaseVo result = new BaseVo();
        if(StringUtils.isBlank(firmwareVersionId) || !StringUtils.isNumeric(firmwareVersionId)){
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        FirmwareVersion firmwareVersion = firmwareVersionService.getFirmwareVersion(Long.parseLong(firmwareVersionId));
        if (firmwareVersion == null) {
            result.setCode(VersionConstant.RESPONSE_FIRMWARE_VERSION_EXIST_ERROR_CODE);
            result.setMsg(VersionConstant.RESPONSE_FIRMWARE_VERSION_EXIST_ERROR_MSG);
        }
        FirmwareVersionInfoVo firmwareVersionInfoVo = new FirmwareVersionInfoVo(firmwareVersion);
        result.setData(firmwareVersionInfoVo);
        return result;
    }
}
